package com.cxyzy.basic_libs

import android.app.Activity
import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide

/**
 * 添加ImageView
 */
fun Activity.显示图片(imageResId: Int, parentView: ViewGroup? = null) =
    showImage(imageResId, parentView)

fun Activity.显示动图(imageResId: Int, parentView: ViewGroup? = null) =
    showImage(imageResId, parentView)

fun Activity.showImage(url: String, parentView: ViewGroup? = null) =
    addView(getImageView(url, this), parentView ?: getContentView(this))

fun Activity.showImage(imageResId: Int, parentView: ViewGroup? = null) =
    addView(getImageView(imageResId, this), parentView ?: getContentView(this))

private fun getImageView(imageResId: Int, context: Context): ImageView {
    val view = ImageView(context)
    Glide.with(context).load(imageResId).placeholder(R.drawable.loading).into(view)
    return view
}

private fun getImageView(imageUrl: String, context: Context): ImageView {
    val view = ImageView(context)
    Glide.with(context).load(imageUrl).placeholder(R.drawable.loading).into(view)
    return view
}